//本页面，是单词测主和词组测评的公用页面，通过level和type来区分（单词or词组）
//获取应用实例
const app = getApp()
const genAudioUrl = "https://api.ai.qq.com/fcgi-bin/aai/aai_tts";
const WxUtil = require('../../../utils/WxUtil.js');
const questionUtil = require('../../../utils/questionUtil.js')
const innerAudioContext = wx.createInnerAudioContext();
//问题个数
const questionNum = 10;
Page({
  data: {
    btn_disabled:false,
    show:false, //加载出题目之前，不显示页面。
    showRight:false,
    showError: false,
    buttonTitle: "下一题",
    checked:false,
    percent: 100/questionNum,
    question_idx: 1,
    questionList: [],
    questionNum: questionNum,
    option_a:"default",
    option_b: "default",
    option_c: "default",
    option_d: "default"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    let type = options.type;
    //查询的数据库名称
    let dbName = type == "word" ? "words" :"word_phrase";
    let level = options.level;
    let count = parseInt(options.count);

    let filter = null;
    //综合单词一
    if (level != "all") {
      level = parseInt(level);
      filter={};
      filter.level = level;
    }

    let totalPage = Math.floor(count / 40);
    //1到totalPage随机
    var page = Math.floor(Math.random() * totalPage)+1;

    wx.showLoading();
    //调用分页云函数
    wx.cloud.callFunction({
      name: 'pagination',
      data: {
        dbName: dbName,
        pageIndex: page,
        pageSize: questionNum*4,
        filter: filter
      },
      success(res) {
        var wordList = res.result.data;

        var questionList = questionUtil.word2Question(wordList); 
        
        console.info("转换完毕");
        console.info(questionList);

        that.setData({
          show: true,
          questionList: questionList
        });
        wx.hideLoading();

        that.playAudio();
      },
      fail() {
        
      }
    });
  },
  nextQuestion: function () {
    let that = this;

    //禁用按钮
    this.setData({ btn_disabled: true });
    //当前题的序号（还未跳至下一题。）
    var idx = this.data.question_idx;

    //该题用户选择的
    var user_select = this.data.questionList[idx - 1].user_select;
    //该题正确答案
    var right_option = this.data.questionList[idx - 1].right;

    if (user_select == right_option)
    {
      this.setData({showRight:true});
    }else
    {
      this.setData({ showError: true });
    }

    //点亮正确答案
    this.lightOptoin();


    //题做完了
    if (idx >= questionNum) {
      app.globalData.userQuetionList.length = 0;
      app.globalData.userQuetionList = this.data.questionList;
      setTimeout(function () {
        wx.redirectTo({
          url: '/pages/tools/result/result'
        });
      },1500);

      return ;
    }
    idx++;
    var percent = (100 / questionNum) * idx;
    var buttonTitle= "下一题";

    if (idx == questionNum)
    {
      buttonTitle = "提交";
    }

    setTimeout(function(){

      //设data,页面跳至下一题。
      that.setData({
        btn_disabled:false,
        buttonTitle: buttonTitle,
        question_idx: idx,
        percent: percent,
        checked: false,
        showRight: false,
        showError: false,
        option_a: "default",
        option_b: "default",
        option_c: "default",
        option_d: "default"
      });
      that.playAudio();
    }, 2000);
    
  },

  /**
   * 选择答案
   */
  selectOption: function (e) {

    //记录用户选择的答案
    var user_select = e.currentTarget.dataset.id;
    var question_idx = this.data.question_idx;
    this.data.questionList[question_idx - 1].user_select = user_select;

  },

  //点亮正确选项
  lightOptoin:function()
  {
    //当前题的序号（还未跳至下一题。）
    var idx = this.data.question_idx;
    this.data.questionList[idx - 1];

    
    //该题正确答案
    var right_option = this.data.questionList[idx - 1].right;
    var rightAnswer = "rightOption";
    if (right_option=="A") {
      this.setData({ option_a: rightAnswer});
    }
    if (right_option == "B") {
      this.setData({ option_b: rightAnswer });
    }
    if (right_option == "C") {
      this.setData({ option_c: rightAnswer });
    }
    if (right_option == "D") {
      this.setData({ option_d: rightAnswer });
    }
  },

  

  //播放音频
  playAudio() {
    try{
      var idx = this.data.question_idx;
      innerAudioContext.src = this.data.questionList[idx - 1].audio;
      innerAudioContext.play();
    }catch(e)
    {
      console.error("播放音频不成功");
    }
  },

  //转化1234为ABCD
  parseRightNum: function (num) {
    var right = "";
    switch (num) {
      case 1:
        right = "A";
        break;
      case 2:
        right = "B";
        break;
      case 3:
        right = "C";
        break;
      case 4:
        right = "D";
        break;
      case 5:
        right = "E";
        break;
      default:
    }

    return right;
  }

})